import { defineConfig , loadEnv } from "vite";
import vue from '@vitejs/plugin-vue'
import viteCompression from 'vite-plugin-compression' 
import { createSvgIconsPlugin, createSymbolId} from 'vite-plugin-svg-icons'
import path, { resolve } from 'node:path'
import Components from 'unplugin-vue-components/vite'   // https://www.npmjs.com/package/unplugin-vue-components
import AutoImport from 'unplugin-auto-import/vite'      // https://www.npmjs.com/package/unplugin-auto-import
import VueMacros from 'unplugin-vue-macros/vite' 
import VueRouter from 'unplugin-vue-router/vite'


// https://vitejs.cn/vite5-cn/config/shared-options.html
export default defineConfig(({command,mode,isSsrBuild,isPreview})=>{
    // serve development false false
    console.log(4,command,mode,isSsrBuild,isPreview)
    let env = loadEnv(mode,process.cwd(),'')
    // console.log(7,env)
    return {
        root:resolve(__dirname,'./') ,
        base : '/' ,
        publicDir : resolve(__dirname , 'public') ,
        build : {
            outDir : resolve(__dirname,'build-vite') , 
            // 指定静态资源存放目录
            assetsDir: "",
            // 启用、禁用css代码拆分
            cssCodeSplit: true,
            // 构建是否生成source map文件
            sourcemap: "inline",
            // rollup 配置打包项
            rollupOptions : {
                input:{
                    index: resolve(__dirname,'index.html'),
                    // demo : resolve(__dirname,'pages/demo.html'), 
                    demo : resolve(__dirname,'demo.html'), 
                },
                output : {
                    entryFileNames : `assets/[name].js` ,
                    chunkFileNames : `assets/[name].js` ,
                    assetFileNames : `assets/[name].[ext]`
                }
            },
            emptyOutDir:false
        },
        
        mode ,
        define : {} ,
        plugins : [
            // vue() , VueMacros 里面有用到
            viteCompression({
                algorithm:'gzip' ,
                threshold : 1024*1e1 ,
                deleteOriginFile:false 
            }),
            createSvgIconsPlugin({
                iconDirs :[path.resolve(process.cwd(),'src/assets/icons')] ,
                symbolId :'icon-[dir]-[name]'
            }),
            VueMacros ({
                defineOptions:false ,
                defineModels : false ,
                plugins:{
                    vue:vue({
                        script : {
                            propsDestructure:true ,
                            defineModel:true
                        }
                    })
                }
            }),
            VueRouter({
                extensions:['.vue'] ,
                importMode : 'async' ,
                routesFolder : [
                    {
                        src :'src/pages' 
                    }
                ],
                logs : true 
            }),
            //  ????? 
            AutoImport({
                imports : [
                    'vue' ,
                    '@vueuse/core' ,
                    {
                        'vue-router/auto':['useLink']
                    }
                ],
                dts : true ,
                dirs : [] ,
                vueTemplate:true 
            }),
            Components({ dts : true }) ,

        ] ,
        
        // cacheDir :'node_modules/.vite'  // 缓存文件目录 
        resolve : {
            alias : {
                '@/':`${path.resolve(__dirname,'src')}/`
            },
            dedupe : [] ,
            conditions :[] ,
            // mainFields : [] 
            extensions :  ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'] , // 导入时想要省略的扩展名列表 
            preserveSymlinks : false 
        },
        html:{
            // cspNonce :'' 
        },
        // 服务器选项
        server : {
            host : true ,
            port : 8000 ,
            // https :  可以添加 @vitejs/plugin-basic-ssl 到项目插件中 
            open : false , 
            proxy : {} ,
            cors : true ,
            // headers : {} ,
            hmr : {}, 
            warmup : {
                clientFiles : [] ,
                ssrFiles : []
            },
            // watch : null , // watch 设置不好 就无法使用 hmr 热更新
            middlewareMode : false ,
            fs : {
                strict:true ,
                allow : ['..'] ,
                deny : ['.env', '.env.*', '*.{crt,pem}'] ,
                cachedChecks : false ,
                // origin : '' ,
            },
            sourcemapIgnoreList : (sourcePath) => sourcePath.includes('node_modules') ,

        },
        // 预览选项
        preview : {
            host : '0.0.0.0' ,
            port : 8001 ,
            strictPort : false ,
            // https : server.https , 
            open : false ,
            // proxy : { } ,
            cors : true ,
            // headers : {} ,
        },
        css : {
            preprocessorOptions : {
                scss:{
                    api:'modern-compiler',
                    additionalData: `$injectedColor: orange;`,
                }
            }
        },
        
    }
})